﻿using System.Linq;
using System.Windows;
using System.Windows.Input;
using Faz.SideWinderSC.Client.ViewModel;
using System.Windows.Controls;

namespace Faz.SideWinderSC.Client
{
    /// <summary>
    /// Defines the view used to configure the profiles of the controllers as well as the controllers themselves.
    /// </summary>
    public partial class ConfigurationWindow : BaseWindow
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigurationWindow"/> class.
        /// </summary>
        public ConfigurationWindow()
        {
            this.InitializeComponent();

            // Data context defined after initialize to ensure the proper initialization
            // of the mapping list - by default the first element is selected whatever was
            // the initial value of the associated binding element.
            this.DataContext = new ConfigurationViewModel();
            this.profiles.SelectionChanged += this.OnProfilesSelectionChanged;
        }

        /// <summary>
        /// Gets the associated view model.
        /// </summary>
        protected ConfigurationViewModel ViewModel
        {
            get { return this.DataContext as ConfigurationViewModel; }
        }

        /// <summary>
        /// Called when the user starts the controller.
        /// </summary>
        /// <param name="sender">The parameter is not used.</param>
        /// <param name="e">The parameter is not used.</param>
        private void OnStartExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            ExecutionViewModel viewModel = new ExecutionViewModel();

            // Activate the execution window
            ExecutionWindow window = new ExecutionWindow() { DataContext = viewModel };
            App.Current.MainWindow = window;
            App.Current.MainWindow.Show();
            this.Close();
        }

        private void OnProfilesSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            bool requiredSave = e.RemovedItems.Cast<ProfileViewModel>().Any(p => p.IsModified);
            if (!requiredSave)
            {
                return;
            }

            string message = "The profile has been modified, would you save your updates?";
            var result = MessageBox.Show(message, "Save", MessageBoxButton.YesNo, MessageBoxImage.Information, MessageBoxResult.Yes);

            switch (result)
            {
                case MessageBoxResult.OK:
                case MessageBoxResult.Yes:
                    foreach (ProfileViewModel profile in e.RemovedItems)
                    {
                        profile.Save.Execute(null);
                    }
                    break;

                case MessageBoxResult.No:
                    foreach (ProfileViewModel profile in e.RemovedItems)
                    {
                        profile.Restore.Execute(null);
                    }
                    break;

                case MessageBoxResult.None:
                case MessageBoxResult.Cancel:
                    break;
            }
        }
    }
}
